package com.ren.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ren.bean.OrderDto;
import com.ren.order.bean.Order;
import com.ren.order.service.IOrderService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ren
 * @since 2022-04-18
 */
@RestController
public class OrderController {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    IOrderService orderService;
    @PostMapping("placeOrder")
    public boolean placeOrder(@RequestBody OrderDto orderDto){
        Order order=new Order();
        BeanUtils.copyProperties(orderDto,order);
        boolean save = orderService.save(order);
        orderDto.setOrderid(order.getOrderid());
        rabbitTemplate.convertAndSend("directExchange001","movie.buy",orderDto);
        return save;
    }
    @GetMapping("findOrder/{userCode}")
    public List<OrderDto> findOrder(@PathVariable("userCode")String userCode){
        QueryWrapper<Order> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userCode",userCode);
        List<Order> list = orderService.list(queryWrapper);
        List<OrderDto> list1=new ArrayList<>();
        for (Order o :
                list) {
            OrderDto orderDto=new OrderDto();
            BeanUtils.copyProperties(o,orderDto);
            list1.add(orderDto);
        }
        return list1;
    }
    @GetMapping("saveOrder/{tag}/{orderId}")
    public boolean saveOrder(@PathVariable("tag")int tag,@PathVariable("orderId")int orderId){
        Order byId = orderService.getById(orderId);
        byId.setTag(tag);
        boolean b = orderService.updateById(byId);
        return b;
    }

}
